﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iacym.Entities;
using System.Data;

namespace Iacym.Repository
{
    public class RolRepository : MasterRepository, IRolRepository
    {
        public List<ROL> GetAllFromRol()
        {
            var query = from c in context.ROLs
                        select c;
            return query.ToList();
        }

        public List<ROL> GetAllFromRolByDescripcion(string criterio)
        {
            if (criterio != string.Empty)
            {
                var query = from c in context.ROLs
                            where c.Descripcion.ToUpper().Contains(criterio.ToUpper())
                            select c;

                return query.ToList();
            }
            else
            {
                var query = from c in context.ROLs
                            select c;

                return query.ToList();
            }
        }

        public ROL GetFromRolById(int id)
        {
            var query = from c in context.ROLs
                        where c.Id.Equals(id)
                        select c;
            return query.SingleOrDefault();
        }

        public void AddRol(ROL rol)
        {
            context.ROLs.Add(rol);
            context.SaveChanges();
        }

        public void UpdateRol(ROL rol)
        {
            context.Entry(rol).State = EntityState.Modified;
            context.SaveChanges();
        }

        public void DeleteRol(int id)
        {
            var existe = context.ROLs.Where(c => c.Id.Equals(id)).SingleOrDefault();
            if (existe != null)
            {
                context.ROLs.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool ExisteDuplicidad(string descripcion, int id)
        {
            var query = from c in context.ROLs
                        where c.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0
                        && c.Id != id
                        select c;

            return query.Any();
        }
    }
}
